//
//  ProjectListViewController.m
//  OAProject
//
//  Created by yhj on 2017/3/8.
//  Copyright © 2017年 cdnunion. All rights reserved.
//  项目列表主界面

#import "ProjectListViewController.h"
#import "ProjectTableViewCell.h"
#import "ProjectModel.h"
#import "EditProjectViewController.h"
#import "AddProjectViewController.h"

static NSString *tableViewCellIdentifier=@"tableViewCellIdentifier";
@interface ProjectListViewController ()<UITableViewDataSource,UITableViewDelegate,ProjectTableViewCellDelegate>

@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)NSMutableArray *projectArr;

// 当前页
@property (nonatomic,assign)int currentPage;

@end

@implementation ProjectListViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    self.projectArr=[NSMutableArray array];

    [self addHeader];

    [self addFooter];

}

-(void)addHeader
{
    WEAKSELF;
    self.tableView.mj_header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _currentPage=1;

        [self.projectArr removeAllObjects];

        [weakSelf getProjectListData];
    }];
     [self.tableView.mj_header beginRefreshing];
}

-(void)addFooter
{
    WEAKSELF;
    MJRefreshAutoNormalFooter *footer=[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _currentPage += 1;
        [weakSelf getProjectListData];
    }];
    [footer setTitle:@"我是有底线的" forState:MJRefreshStateNoMoreData];
    footer.automaticallyHidden=YES;
    self.tableView.mj_footer=footer;
}


// 编辑项目
-(void)editProjectWithProjectModel:(ProjectModel *)model cell:(ProjectTableViewCell *)cell
{
    EditProjectViewController *vc=[EditProjectViewController new];
    vc.model=model;
    [self.navigationController pushViewController:vc animated:NO];
}

// 删除项目
-(void)deleteProjectWithProjectModel:(ProjectModel *)model cell:(ProjectTableViewCell *)cell
{
    UIAlertView *alert=[UIAlertView alertViewWithTitle:@"提示" message:@"你确定要删除它吗？" cancelButtonTitle:@"取消" didDismissBlock:^(UIAlertView *alertView,NSInteger buttonIndex) {

        if (buttonIndex==0)
        {
            [API DeleteProjectWithProjectID:model.projectID success:^(id res) {

                [self addHeader];
                [self addFooter];

            } failure:^(NSInteger stateCode) {

            }];
        }
    } otherButtonTitles:@"确定", nil];
    [alert show];
}

-(void)addProjectBtn
{
    UIButton *addBtn=[AppUI createButtonWithSupeView:self.view andWithNormalImage:[UIImage imageNamed:@"add"] andWithSelectImage:[UIImage imageNamed:@"add"] andWithBtnAction:^{

        // 跳转到 项目添加 界面
        AddProjectViewController *vc=[AddProjectViewController new];
        [self.navigationController pushViewController:vc animated:NO];

    }];
    [self.view addSubview:addBtn];
    addBtn.frame=CGRectMake(APPW-kmargin*8,APPH-kmargin*10,kmargin*6,kmargin*6);
}


- (void)viewDidLoad
{
    [super viewDidLoad];

    self.title=@"项目";

    [self.view addSubview:self.tableView];

    [self addProjectBtn];

}

-(void)getProjectListData
{
    WEAKSELF;
    [API GetProjectListWithPer_page:KCountNumber page_num:[NSString stringWithFormat:@"%d",_currentPage] success:^(id res)
    {
        [weakSelf endRefresh:self.tableView array:self.projectArr];

        if ([res[@"rt"] isEqual:@(1)])
        {
            NSArray *array=res[@"rows"];

            for (NSDictionary *dic in array)
            {
                ProjectModel *model=[ProjectModel yy_modelWithDictionary:dic];
                [weakSelf.projectArr addObject:model];
            }

            if (self.currentPage<[res[@"page"][@"pages"] integerValue])
            {
                [weakSelf.tableView.mj_footer resetNoMoreData];
            }
            else
            {
                [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
            }
        }
        else
        {
            [self.view makeToast:res[@"error"] duration:1 position:CSToastPositionCenter];
        }

        [weakSelf.tableView reloadData];

    } failure:^(NSInteger stateCode)
    {
        [weakSelf requestFail:self.tableView array:self.projectArr];
    }];
}

#pragma mark 上下拉刷新
- (void)endRefresh:(UITableView *)tableView array:(NSMutableArray *)array
{
    if ([tableView.mj_header isRefreshing])
    {
        [array removeAllObjects];
        [tableView.mj_header endRefreshing];
    }
    if ([tableView.mj_footer isRefreshing])
    {
        [tableView.mj_footer endRefreshing];
    }
}

// 收到请求失败的通知
- (void)requestFail:(UITableView *)tableView array:(NSMutableArray *)array
{
    if ([tableView.mj_header isRefreshing])
    {
        [tableView.mj_header endRefreshing];
    }
    if ([tableView.mj_footer isRefreshing])
    {
        [tableView.mj_footer endRefreshing];
    }
    [array removeAllObjects];
    [tableView reloadData];
}

// tableView
-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0,0,APPW,APPH) style:UITableViewStylePlain];
        _tableView.dataSource=self;
        _tableView.delegate=self;
        _tableView.backgroundColor=BGColor;
        _tableView.showsVerticalScrollIndicator=NO;
        _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        [self.tableView registerClass:[ProjectTableViewCell class] forCellReuseIdentifier:tableViewCellIdentifier];
    }
    return _tableView;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.projectArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ProjectTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:tableViewCellIdentifier];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    cell.delegate=self;
    cell.projectModel=self.projectArr[indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kmargin*9;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footView=[UIView new];
    return footView;
}


-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return kmargin;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    ProjectModel *model=self.projectArr[indexPath.row];
    if (self.delegate&&[self.delegate respondsToSelector:@selector(selectProjectName:projectID:)])
    {
        [self.delegate selectProjectName:model.project_name projectID:model.projectID];
    }
    [self.navigationController popViewControllerAnimated:NO];
}

@end
